package com.wang.head.controller;

import com.wang.common.controller.BaseController;
import com.wang.common.util.ConstatFinalUtil;
import com.wang.common.util.PageInfoUtil;
import com.wang.goods.pojo.ACate;
import com.wang.goods.pojo.AGoods;
import com.wang.goods.service.IGoodsService;
import com.wang.users.service.IUsersService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 不需要登陆就可以访问的页面
 */
@Controller
public class IndexHeadController extends BaseController
{
    @Resource
    private IUsersService usersService;
    @Resource
    private IGoodsService goodsService;

    /**
     * 首页
     * @return
     */
    @RequestMapping("/index")
    public String index()
    {
        ConstatFinalUtil.SYS_LOGGER.info("==index===");
        return "/head/index" ;
    }

    /**
     * 商品列表
     * @return
     */
    @RequestMapping("/goodsList")
    public String goodsList(HttpServletRequest request, Model model)
    {
        ConstatFinalUtil.SYS_LOGGER.info("==goodsList===");
        Map<String,Object> condMap = new HashMap<String,Object>();
        /* 处理搜索条件 */
        this.searchCond(request,condMap);

        /* cateId */
        String cateId = request.getParameter("cateId") ;
        if(cateId == null)
        {
            cateId = "" ;
        }
        condMap.put("cateId",cateId);
        request.setAttribute("cateId",cateId);

        PageInfoUtil pageInfoUtil =  this.pageInfoProcced(request);

        /* 查询数据库 */
        List<AGoods> goodsList = this.goodsService.selectCondListGoodsService(pageInfoUtil,condMap);

        /* 放到四大作用域(Servlet)
         * pageContext,request,session,application */
        request.setAttribute("list",goodsList);
        /* 以下两行代码效果一样 */
        //request.setAttribute("pageInfoUtil",pageInfoUtil);
        /*SpringMVC的 */
        model.addAttribute("pageInfoUtil",pageInfoUtil);

        /* 查询分类表中所有启用的Cate */
        condMap.clear();
        condMap.put("status","1");
        List<ACate> cateList = this.goodsService.selectCondListCateService(null, condMap);
        request.setAttribute("cateList",cateList);

        return "/head/goodsList" ;
    }
}
